using CfgTable;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_MallItemPrice : UIBaseNode
    {
        #region Template Generate,don't modify
        protected class UIB_UIN_MallItemPrice
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExText textTitle { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textValue { protected set; get; }
            public UnityEngine.GameObject sale { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textSale { protected set; get; }
            public IQIGame.Onigao.Framework.ExImage icon { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTitle", out var __tbv0);
                this.textTitle = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textValue", out var __tbv1);
                this.textValue = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("sale", out var __tbv2);
                this.sale = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textSale", out var __tbv3);
                this.textSale = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExImage>("icon", out var __tbv4);
                this.icon = __tbv4;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields



        #endregion fields

        #region properties

        protected UIB_UIN_MallItemPrice ui { get; private set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_MallItemPrice();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {

        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methond

        public void Show(CfgMallItem cfgMallItem)
        {
            this.Show();
            this.ui.textTitle.SetActive(false);
            this.ui.sale.SetActive(false);
            this.ui.icon.SetActive(false);

            switch (cfgMallItem.SellType)
            {
                case MallSellType.Free:
                    this.ShowFree();
                    break;
                case MallSellType.Item:
                    this.ShowItem(cfgMallItem.Price[0], cfgMallItem.Price[1], cfgMallItem.Discount);
                    break;
                case MallSellType.PayPoint:
                    this.ShowPayPoint(cfgMallItem.PayPoint, cfgMallItem.Discount);
                    break;
                case MallSellType.Recharge:
                    this.ShowRecharge(cfgMallItem.Amount);
                    break;
            }
        }

        private void ShowFree()
        {
            this.ui.textValue.text = APIMall.Free;
        }

        private void ShowRecharge(float itemAmount)
        {
            this.ui.textTitle.SetActive(true);
            this.ui.textTitle.text = APIMall.RechargeTitle;
            this.ui.textValue.text = itemAmount.ToString();
        }

        private void ShowPayPoint(int salePrice, int realPrice)
        {
            this.ShowItem(ItemConst.ItemCid_PayPoint_ForShow, salePrice, realPrice);
        }

        private void ShowItem(int itemCid, int salePrice, int realPrice)
        {
            this.ui.icon.SetActive(true);

            var cfgItem = TableCenter.item.Get(itemCid);
            this.SetImageSprite(this.ui.icon, cfgItem.Icon_fullPath);
            bool isActiveRealPrice = realPrice > 0;
            if (isActiveRealPrice)
            {
                this.ui.textSale.text = realPrice.ToString();
            }
            this.ui.sale.SetActive(isActiveRealPrice);
            this.ui.textValue.text = salePrice.ToString();
        }
        #endregion methonds
    }
}
